package com.charonmc;

import com.charonmc.commands.*;
import com.charonmc.listeners.PlayerListenerCore;
import com.charonmc.utils.MessageUtils;
import com.charonmc.utils.PacketManager;
import com.charonmc.utils.VanishUtils;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;

public class CharonCore extends JavaPlugin {
    public static CharonCore instance = null;
    private static ProtocolManager pm;
    public static ProtocolManager getProtocolManager() {
        return pm;
    }
    @Override
    public void onEnable() {
        instance = this;
        this.getCommand("Vanish").setExecutor(new Vanish());
        this.getCommand("Fly").setExecutor(new Fly());
        this.getCommand("Nick").setExecutor(new Nick());
        this.getCommand("UnNick").setExecutor(new UnNick());
        BukkitRunnable sendVanishMessage = new BukkitRunnable() {
            @Override
            public void run() {
                for (Player x : VanishUtils.getVanishPlayers()) {
                    MessageUtils.sendActionBar(x, VanishUtils.getChatBaseComponent());
                }
            }
        };
        sendVanishMessage.runTaskTimer(this, 10L, 10L);
        Bukkit.getPluginManager().registerEvents(new PlayerListenerCore(), this);
        pm = ProtocolLibrary.getProtocolManager();
        pm.addPacketListener(new PacketManager());
    }
    @Override
    public void onDisable() {
        this.getServer().getScheduler().cancelTasks(this);
        getLogger().info("§c§l[Core]已卸载。");
    }

    public static CharonCore getInstance(){
        return instance;
    }
}
